package com.hadwinling.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hadwinling.blog.entity.*;
import com.hadwinling.blog.service.*;
import com.hadwinling.blog.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.hadwinling.blog.controller
 * @date 2020/8/20 8:58
 */
@Controller
public class IndexController {
    @Autowired
    private TagService tagService;
    @Autowired
    private DayService dayService;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private PhotoService photoService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private CommentService commentService;

    @Value("${admin.article.page.pageSize}")
    private int pageSize;
    @Value("${page.navigatePage}")
    private int navigatePage;


    /**
     * 首页
     * @return
     */
    @GetMapping("/")
    public String index(Model model){
        //找到置顶的tag
        List<Tag> topList = tagService.selectAllByTopAndState(1, 1);
        model.addAttribute("topList",topList);
        //找到置顶的每日一言
        List<Day> dayList = dayService.selectAllByStateAndTop(1,1);
        model.addAttribute("topDayList",dayList);
        //找到置顶的公告0:表示 首页
        List<Notice> topNoticeList =  noticeService.selectAllByTopAndStateAndPowerstate(1,1,0);
        model.addAttribute("topNoticeList",topNoticeList);
        //找到置顶的文章
        List<Article> topArticleList = articleService.selectAllByStateAndTop(1,1);
        model.addAttribute("topArticleList",topArticleList);

        return "index";
    }

    /**
     * 跳转到美图页面
     * @return
     */
    @GetMapping("/photo")
    public String toPhotoPage(){
        return "photo";
    }

    /**
     * 美图数据加载
     * @param pageNum
     * @return
     */
    @PostMapping("/photo")
    @ResponseBody
    public Msg photo(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum){
        PageHelper.startPage(pageNum,6);
        List<Photo> photoList = photoService.selectAllByState(1);
        PageInfo<Photo> pageInfo = new PageInfo<>(photoList,navigatePage);
        return Msg.success().add("pageInfo",pageInfo);
    }

    /**
     * 跳转到美文的页面
     * @param pageNum
     * @param model
     * @return
     */
    @GetMapping("/article")
    public String toArticlePage(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum,Model model){
        List<Tag> tagList = tagService.selectAllByState(1);
        model.addAttribute("tagList",tagList);
        PageHelper.startPage(pageNum,pageSize);
        List<Article> articleList = articleService.selectAllByState(1);
        PageInfo<Article> pageInfo = new PageInfo<>(articleList,navigatePage);
        //当前页码
        int currPage = pageInfo.getPageNum();
        //总记录
        Long total = pageInfo.getTotal();
        //总页数
        int totalPage = pageInfo.getPages();
        //所有页面
        int [] pages = pageInfo.getNavigatepageNums();
        //保存分页信息
        model.addAttribute("currPage",currPage);
        model.addAttribute("total",total);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pages",pages);
        model.addAttribute("articleList",articleList);
        return "article";
    }

    /**
     * 查看文章详情
     * @param articleid
     * @param model
     * @return
     */
    @GetMapping("/article/{articleid}")
    public String checkArticle(@PathVariable(name = "articleid") int articleid,Model model){
        Article article = articleService.selectAllByStateAndId(1,articleid);
        model.addAttribute("article",article);
//        System.out.println("article:"+article);
        //对其浏览量进行增加
        article.setVisitcount(article.getVisitcount()+1);
        articleService.updateByPrimaryKey(article);
        //找到该作者的其他作品
        int adminId = article.getAdminid();
      /*  //找到同一个作者的5 条数据
        List<Article> moreArtileList = articleService.selectAllByAdminid(adminId,5);
        if (moreArtileList.size()!=0){
            model.addAttribute("moreArtileList" ,moreArtileList);
        }else{
            model.addAttribute("moreArtileList" ,-1);
        }*/

     /*   //找到同一类别的5条数据
        int tagid = article.getTagid();
        List<Article> tagArticleList = articleService.selectAllByStateAndTagidAndLimit(1,tagid,5);
        if(tagArticleList.size()!=0){
            model.addAttribute("tagArticleList",tagArticleList);
        }else{
            model.addAttribute("tagArticleList",-1);
        }*/
        //根据文章id找到所有评论
        List<Comment> commentList = commentService.selectAllByArticleidOrderByCreatetimeDesc(articleid);
        model.addAttribute("commentList",commentList);
        //根据某个文章id找到评论数
        int commentNum = commentList.size();
        model.addAttribute("commentNum",commentNum);

        return "articleDetail";
    }

    /**
     * 查看文章类别对应的文章
     * @param tagid
     * @param model
     * @param pageNum
     * @return
     */
    @GetMapping("/checkTag/{tagid}")
    public String checkArticleByTagId(@PathVariable(name = "tagid") int tagid
            ,Model model
            ,@RequestParam(name = "pageNum",defaultValue = "1") int pageNum){

        List<Tag> tagList = tagService.selectAllByState(1);
        model.addAttribute("tagList",tagList);
        PageHelper.startPage(pageNum,pageSize);
        List<Article> articleList = articleService.selectAllByStateAndTagid(1,tagid);
        PageInfo<Article> pageInfo = new PageInfo<>(articleList,navigatePage);
        //当前页码
        int currPage = pageInfo.getPageNum();
        //总记录
        Long total = pageInfo.getTotal();
        //总页数
        int totalPage = pageInfo.getPages();
        //所有页面
        int [] pages = pageInfo.getNavigatepageNums();
        //保存分页信息
        model.addAttribute("currPage",currPage);
        model.addAttribute("total",total);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pages",pages);
        model.addAttribute("articleList",articleList);
        model.addAttribute("tagid",tagid);
        return "articleTag";
    }

    /**
     * 美文数据加载
     * @param pageNum
     * @return
     */
    @PostMapping("/article")
    @ResponseBody
    public Msg article(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum){
        PageHelper.startPage(pageNum,3);
        List<Article> articleList = articleService.selectAllByState(1);
        PageInfo<Article> pageInfo = new PageInfo<>(articleList,navigatePage);
        return Msg.success().add("pageInfo",pageInfo);
    }


    /**
     * 用户详情
     * @param userid
     * @param model
     * @return
     */
    @GetMapping("/userInfo/{userid}")
    public String toPersonCenterPage(@PathVariable(name = "userid") int userid,Model model){
        Admin user = adminService.selectByPrimaryKey(userid);
        model.addAttribute("user",user);
        //根据用户id统计他一共收到多少个点赞数
        int likeNum =articleService.countLikeById(userid);
        model.addAttribute("likeNum",likeNum);
        return "userInfo";
    }

    /**
     * 美文数据加载
     * @param pageNum
     * @return
     */
    @PostMapping("/userArticle")
    @ResponseBody
    public Msg Userarticle(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum
            ,@RequestParam(name = "userid") int userid){
        PageHelper.startPage(pageNum,3);
        List<Article> articleList = articleService.selectAllByAdminidAndState(userid,1);
        PageInfo<Article> pageInfo = new PageInfo<>(articleList,navigatePage);
        return Msg.success().add("pageInfo",pageInfo);
    }




}
